package com.paperobot.action;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.paperobot.common.util.PageUtil;
import com.paperobot.common.util.VOHelper;
import com.paperobot.model.Magazine;
import com.paperobot.model.News;
import com.paperobot.model.Paper;
import com.paperobot.model.PaperType;
import com.paperobot.model.vo.PaperVO;
import com.paperobot.service.PaperobotService;

public class PaperUIAction extends BaseAction{

	private static final long serialVersionUID = 1516834338392982332L;
	private PaperobotService paperobotService;;
	private Paper paper;
	private Magazine magazine;
	private PaperVO paperVO;
	private News news;
	private List<Paper> paperList = new ArrayList<Paper>();
	private List<Paper> findPaperList = new ArrayList<Paper>();
	private List<PaperVO> typePaperVOList = new ArrayList<PaperVO>();
	private List<PaperVO> wkbyPaperVOList = new ArrayList<PaperVO>();
	private List<PaperVO> lkbyPaperVOList = new ArrayList<PaperVO>();
	private List<PaperVO> gkbyPaperVOList = new ArrayList<PaperVO>();
	private List<PaperVO> yxbypaperVOList = new ArrayList<PaperVO>();
	private List<PaperVO> dxbypaperVOList = new ArrayList<PaperVO>();
	private List<PaperVO> dxqkpaperVOList = new ArrayList<PaperVO>();
	private List<PaperVO> hotPaperVOList = new ArrayList<PaperVO>();
	private List<PaperVO> recommendPaperVOList = new ArrayList<PaperVO>();
	private PageUtil pageUtil = new PageUtil();
	private List<PaperType> paperTypeList;
	private List<PaperType> paperSubTypeList;
	private List<News> newsList = new ArrayList<News>();
	private List<News> hotNewsList = new ArrayList<News>();
	private List<News> recommendNewsList = new ArrayList<News>();
	private List<Magazine> magazineList = new ArrayList<Magazine>();
	private PaperType paperType = new PaperType();
	
	private String title;
	private int TYPE = -1;
	private int SUB_TYPE = -1;
	private final int NULL_TYPE = -1;
	private final int WKBY_TYPE = 1;
	private final int LKBY_TYPE = 2;
	private final int GKBY_TYPE = 3;
	private final int YXBY_TYPE = 4;
	private final int DXBY_TYPE = 5;
	private final int DXQK_TYPE = 6;
	private final boolean STATUS=true;
	private final int TOP_NUM = 15;
	
	
	@SuppressWarnings("unchecked")
	public String initMain(){
		try {
			wkbyPaperVOList = VOHelper.convertPaperTitle(getPaperVOList("first", WKBY_TYPE, NULL_TYPE));
			lkbyPaperVOList = VOHelper.convertPaperTitle(getPaperVOList("first", LKBY_TYPE, NULL_TYPE));
			gkbyPaperVOList = VOHelper.convertPaperTitle(getPaperVOList("first", GKBY_TYPE, NULL_TYPE));
			yxbypaperVOList = VOHelper.convertPaperTitle(getPaperVOList("first", YXBY_TYPE, NULL_TYPE));
			dxbypaperVOList = VOHelper.convertPaperTitle(getPaperVOList("first", DXBY_TYPE, NULL_TYPE));
			dxqkpaperVOList = VOHelper.convertPaperTitle(getPaperVOList("first", DXQK_TYPE, NULL_TYPE));
			
			//
			List<News> allNewsList = VOHelper.convertNewsTitle(paperobotService.getNewsList(STATUS));
			newsList = pageUtil.getCurrentPageRows("first", allNewsList.size(), allNewsList);
			
			//
			List<Magazine> allMagazineList = paperobotService.getMagazineList(STATUS);
			magazineList = pageUtil.getCurrentPageRows("first", allMagazineList.size(), allMagazineList);
			
			logger.info("user > " + request.getRemoteAddr() + " : " + request.getRemoteHost() + "   visit initMain. ");
			
			return SUCCESS;
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}
	
	
	public String getTypePaperList(){
		try {
			String pagetype = request.getParameter("pagetype");
			if(pagetype == null){
				pagetype = "first";
			}
			
			String currentPage = request.getParameter("currentPage");
			if(currentPage != null){
				pageUtil.setCurrentPage(Integer.parseInt(currentPage));
			}
			
			String type = request.getParameter("type");
			if(type != null){
				TYPE = Integer.parseInt(type);
			}
			
			String subType = request.getParameter("subType");
			if(subType != null){
				SUB_TYPE = Integer.parseInt(subType);
			}
			
			if(NULL_TYPE != TYPE ){
				
				getOtherMessageByType(TYPE);
				//
				if(NULL_TYPE == SUB_TYPE){
					logger.info("TYPE: " + TYPE);
					typePaperVOList = getPaperVOList(pagetype, TYPE, NULL_TYPE);
					hotPaperVOList = getHotPaperVOList("first", TYPE, NULL_TYPE);
					recommendPaperVOList = getRecommendPaperVOList("first", TYPE, NULL_TYPE);
				}else{
					logger.info("TYPE: " + TYPE + "  SUB_TYPE:  " + SUB_TYPE);
					typePaperVOList = getPaperVOList(pagetype, TYPE, SUB_TYPE);
					hotPaperVOList = getHotPaperVOList("first", TYPE, SUB_TYPE);
					recommendPaperVOList = getRecommendPaperVOList("first", TYPE, SUB_TYPE);
				}
				
				logger.info("user > " + request.getRemoteAddr() + " : " + request.getRemoteHost() + "   visit getTypePaperList. ");
				
				return "articlelist";
			}else{
				return ERROR;
			}
			
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}

	public void getOtherMessageByType(int type){
		paperSubTypeList = paperobotService.getPaperTypeList(type);
	}
	
	public String getLwdx(){
		try {
			String type = request.getParameter("lwdx");
			logger.info("user > " + request.getRemoteAddr() + " : " + request.getRemoteHost() + "  "+ type + "   visit getLwdx. ");
			
			if(type == null){
				return "lwdz";
			}else if(type.equals("lwdz")){
				return "lwdz";
			}else if(type.equals("fkfs")){
				return "fkfs";
			}else if(type.equals("gywm")){
				return "gywm";
			}else if(type.equals("cyjm")){
				return "cyjm";
			}else if(type.equals("lxwm")){
				return "lxwm";
			}else if(type.equals("hzqk")){
				return getListMagazine();
			}else{
				return ERROR;
			}
			
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}
	
	@SuppressWarnings("unchecked")
	public String getListMagazine(){
		try {
			String pagetype = request.getParameter("pagetype");
			if(pagetype == null){
				pagetype = "first";
			}
			
			String currentPage = request.getParameter("currentPage");
			if(currentPage != null){
				pageUtil.setCurrentPage(Integer.parseInt(currentPage));
			}
			
			List<Magazine> allMagazineList = paperobotService.getMagazineList(STATUS);
			magazineList = pageUtil.getCurrentPageRows(pagetype, allMagazineList.size(), allMagazineList);
			
			logger.info("user > " + request.getRemoteAddr() + " : " + request.getRemoteHost() + "   visit getListMagazine. ");
			
			return "hzqk";
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}
	
	public String getMagazineById(){
		try {
			String magazineId = request.getParameter("magazineId");
			
			magazine = paperobotService.getMagazine(Long.parseLong(magazineId));

			logger.info("user > " + request.getRemoteAddr() + " : " + request.getRemoteHost() + "   visit getMagazineById. ");
			
			return "magazine";
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}
	
	public String getPaperById(){
		try {
			String paperId = request.getParameter("paperId");
			
			logger.info("user > " + request.getRemoteAddr() + " : " + request.getRemoteHost() + "   visit getPaperById. ");
			
			paper = paperobotService.getPaper(Long.parseLong(paperId));
			if(paper != null){

				checkPaperTypeList(paper.getType(), paper.getSubType());
				hotPaperVOList = getHotPaperVOList("first", paper.getType(), paper.getSubType());
				recommendPaperVOList = getRecommendPaperVOList("first", paper.getType(), paper.getSubType());
				
				paperobotService.updatePaperClickNum(paper.getPaperId());
				
				return "paper";
				
			}else{
				return ERROR;		
			}

		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}
	
	public String getNewsById(){
		try {
			String newsId = request.getParameter("newsId");
			news = paperobotService.getNews(Long.parseLong(newsId));

			hotNewsList = VOHelper.convertNewsTitle(paperobotService.getHotNewsList(STATUS, TOP_NUM));
			
			recommendNewsList = VOHelper.convertNewsTitle(paperobotService.getRecommendNewsList(STATUS, TOP_NUM));
			
			paperobotService.updateNewsClickNum(news.getNewsId());
			
			logger.info("user > " + request.getRemoteAddr() + " : " + request.getRemoteHost() + "   visit getNewsById. ");
			
			return "news";
			
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}
	
	@SuppressWarnings("unchecked")
	public String getListNews(){
		try {
			String pagetype = request.getParameter("pagetype");
			if(pagetype == null){
				pagetype = "first";
			}
			
			String currentPage = request.getParameter("currentPage");
			if(currentPage != null){
				pageUtil.setCurrentPage(Integer.parseInt(currentPage));
			}
			
			List<News> allNewsList = paperobotService.getNewsList(STATUS);
			newsList = pageUtil.getCurrentPageRows(pagetype, allNewsList.size(), allNewsList);
			
			hotNewsList = VOHelper.convertNewsTitle(paperobotService.getHotNewsList(STATUS, TOP_NUM));
			
			recommendNewsList = VOHelper.convertNewsTitle(paperobotService.getRecommendNewsList(STATUS, TOP_NUM));
			
			logger.info("user > " + request.getRemoteAddr() + " : " + request.getRemoteHost() + "   visit getListNews. ");
			
			return "newslist";
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}
	
	@SuppressWarnings("unchecked")
	private List<PaperVO> getPaperVOList(String pagetype, int type, int subType ){
		List<Paper> allPaperList;
		if(NULL_TYPE == subType){
			allPaperList = paperobotService.getPaperList(type, STATUS);
		}else{
			allPaperList = paperobotService.getPaperList(type, subType, STATUS); 
		}
		
		paperList = pageUtil.getCurrentPageRows(pagetype, allPaperList.size(), allPaperList);
		checkPaperTypeList(type, subType);
		
		
		return VOHelper.paperToVO(paperList, paperTypeList);
	}
	
	private List<PaperVO> getHotPaperVOList(String pagetype, int type, int subType ){
		List<Paper> allPaperList;
		if(NULL_TYPE == subType){
			allPaperList = paperobotService.getHotPaperList(type, STATUS, TOP_NUM);
		}else{
			allPaperList = paperobotService.getHotPaperList(type, subType, STATUS, TOP_NUM); 
		}
		
		if(null == paperTypeList){
			paperTypeList = paperobotService.getPaperTypeList();
		}
		
		return VOHelper.convertPaperTitle(VOHelper.paperToVO(allPaperList, paperTypeList));
		
	}
	
	private List<PaperVO> getRecommendPaperVOList(String pagetype, int type, int subType ){
		List<Paper> allPaperList;
		if(NULL_TYPE == subType){
			allPaperList = paperobotService.getRecommendPaperList(type, STATUS, TOP_NUM);
		}else{
			allPaperList = paperobotService.getRecommendPaperList(type, subType, STATUS, TOP_NUM); 
		}
		
		if(null == paperTypeList){
			paperTypeList = paperobotService.getPaperTypeList();
		}
		
		return VOHelper.convertPaperTitle(VOHelper.paperToVO(allPaperList, paperTypeList));
	}
	
	public void checkPaperTypeList(int type, int subType){
		if(null == paperTypeList){
			paperTypeList = paperobotService.getPaperTypeList();
		}
		if(NULL_TYPE == subType){
			for(int i=0; i<paperTypeList.size(); i++){
				if(type == paperTypeList.get(i).getType()){
					paperType.setSubTypeName(paperTypeList.get(i).getTypeName());
					paperType.setTypeName(paperTypeList.get(i).getTypeName());
					paperType.setType(type);
					paperType.setSubType(NULL_TYPE);
					break;
				}
			}
		}else{
			for(int i=0; i<paperTypeList.size(); i++){
				if(subType == paperTypeList.get(i).getSubType()){
					paperType.setSubTypeName(paperTypeList.get(i).getSubTypeName());
					paperType.setTypeName(paperTypeList.get(i).getTypeName());
					paperType.setType(type);
					paperType.setSubType(subType);
					break;
				}
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	public String findPaperList(){
		try {
			String findName = request.getParameter("title");
			if(findName == null){
				title = "";
			}else{
				title = findName;
			}
			
			String pagetype = request.getParameter("pagetype");
			if(pagetype == null){
				pagetype = "first";
			}
			
			String currentPage = request.getParameter("currentPage");
			if(currentPage != null){
				pageUtil.setCurrentPage(Integer.parseInt(currentPage));
			}
			
			List<Paper> paperList = paperobotService.findPaperList(title);
			findPaperList = pageUtil.getCurrentPageRows(pagetype, paperList.size(), paperList);
			
			logger.info("user > " + request.getRemoteAddr() + " : " + request.getRemoteHost() + "   visit findPaperList. ");
			
			return "find";
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}
	
	
	@Resource
	public void setPaperobotService(PaperobotService paperobotService) {
		this.paperobotService = paperobotService;
	}
	public PaperVO getPaperVO() {
		return paperVO;
	}
	public void setPaperVO(PaperVO paperVO) {
		this.paperVO = paperVO;
	}
	public Paper getPaper() {
		return paper;
	}
	public void setPaper(Paper paper) {
		this.paper = paper;
	}
	public List<Paper> getPaperList() {
		return paperList;
	}
	public void setPaperList(List<Paper> paperList) {
		this.paperList = paperList;
	}
	public List<PaperType> getPaperTypeList() {
		return paperTypeList;
	}
	public void setPaperTypeList(List<PaperType> paperTypeList) {
		this.paperTypeList = paperTypeList;
	}
	public List<PaperType> getPaperSubTypeList() {
		return paperSubTypeList;
	}
	public void setPaperSubTypeList(List<PaperType> paperSubTypeList) {
		this.paperSubTypeList = paperSubTypeList;
	}
	public PageUtil getPageUtil() {
		return pageUtil;
	}
	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}
	public List<PaperVO> getWkbyPaperVOList() {
		return wkbyPaperVOList;
	}
	public void setWkbyPaperVOList(List<PaperVO> wkbyPaperVOList) {
		this.wkbyPaperVOList = wkbyPaperVOList;
	}
	public List<PaperVO> getLkbyPaperVOList() {
		return lkbyPaperVOList;
	}
	public void setLkbyPaperVOList(List<PaperVO> lkbyPaperVOList) {
		this.lkbyPaperVOList = lkbyPaperVOList;
	}
	public List<PaperVO> getGkbyPaperVOList() {
		return gkbyPaperVOList;
	}
	public void setGkbyPaperVOList(List<PaperVO> gkbyPaperVOList) {
		this.gkbyPaperVOList = gkbyPaperVOList;
	}
	public List<PaperVO> getYxbypaperVOList() {
		return yxbypaperVOList;
	}
	public void setYxbypaperVOList(List<PaperVO> yxbypaperVOList) {
		this.yxbypaperVOList = yxbypaperVOList;
	}
	public List<PaperVO> getDxbypaperVOList() {
		return dxbypaperVOList;
	}
	public void setDxbypaperVOList(List<PaperVO> dxbypaperVOList) {
		this.dxbypaperVOList = dxbypaperVOList;
	}
	public PaperobotService getPaperobotService() {
		return paperobotService;
	}
	public List<PaperVO> getTypePaperVOList() {
		return typePaperVOList;
	}
	public void setTypePaperVOList(List<PaperVO> typePaperVOList) {
		this.typePaperVOList = typePaperVOList;
	}
	public List<PaperVO> getDxqkpaperVOList() {
		return dxqkpaperVOList;
	}
	public void setDxqkpaperVOList(List<PaperVO> dxqkpaperVOList) {
		this.dxqkpaperVOList = dxqkpaperVOList;
	}
	public List<News> getNewsList() {
		return newsList;
	}
	public void setNewsList(List<News> newsList) {
		this.newsList = newsList;
	}
	public News getNews() {
		return news;
	}
	public void setNews(News news) {
		this.news = news;
	}
	public PaperType getPaperType() {
		return paperType;
	}
	public void setPaperType(PaperType paperType) {
		this.paperType = paperType;
	}
	public List<PaperVO> getHotPaperVOList() {
		return hotPaperVOList;
	}
	public void setHotPaperVOList(List<PaperVO> hotPaperVOList) {
		this.hotPaperVOList = hotPaperVOList;
	}
	public List<PaperVO> getRecommendPaperVOList() {
		return recommendPaperVOList;
	}
	public void setRecommendPaperVOList(List<PaperVO> recommendPaperVOList) {
		this.recommendPaperVOList = recommendPaperVOList;
	}
	public List<News> getHotNewsList() {
		return hotNewsList;
	}
	public void setHotNewsList(List<News> hotNewsList) {
		this.hotNewsList = hotNewsList;
	}
	public List<News> getRecommendNewsList() {
		return recommendNewsList;
	}
	public void setRecommendNewsList(List<News> recommendNewsList) {
		this.recommendNewsList = recommendNewsList;
	}
	public List<Paper> getFindPaperList() {
		return findPaperList;
	}
	public void setFindPaperList(List<Paper> findPaperList) {
		this.findPaperList = findPaperList;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public List<Magazine> getMagazineList() {
		return magazineList;
	}
	public void setMagazineList(List<Magazine> magazineList) {
		this.magazineList = magazineList;
	}
	public Magazine getMagazine() {
		return magazine;
	}
	public void setMagazine(Magazine magazine) {
		this.magazine = magazine;
	}

}
